/**
 * 
 */
package com.et114.modules.products.base;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.et114.components.simpletable.DataList;
import com.et114.core.services.impl.BaseServicesImpl;
import com.et114.core.utility.FileUploadUtil;
import com.et114.core.utility.NumberUtils;
import com.et114.modules.products.commons.Context;
//import com.et114.modules.products.commons.Const;

/*
 * @author guanhw
 */
public class ProImageServiceImpl extends BaseServicesImpl {
	private ProImageDao proImageDao ;

	public void setProImageDao ( ProImageDao proImageDao ) {
		this.proImageDao = proImageDao;
	} 
	
	public List< ProImage > getProImageList( ProImage vo )  throws Exception {
		return proImageDao.getProImageList (  vo ) ; 
	} 
	
	public DataList getProImages( ProImage vo ) throws Exception {
		return proImageDao.getProImages ( vo ) ;
	}
	
	public ProImage getProImageById( Long id  ) throws Exception {
		return proImageDao.getProImageById ( id ) ;
	} 
	
	public Long saveProImage( ProImage vo ) throws Exception {
		return proImageDao.saveProImage ( vo ) ;
	}
	
	public void updateProImage( ProImage vo ) throws Exception {
		if ( NumberUtils.isEmptyOrZero( vo.getDisOrder ( ) ) ) 
			vo.setDisOrder ( vo.getProimgId ( ) ) ;

		proImageDao.updateProImage ( vo ) ;
		
	}
	
	public void removeProImageById( Long id ) throws Exception {
		ProImage image = getProImageById ( id ) ; 
		if ( image != null ) {
			if ( ! StringUtils.isEmpty ( image.getProp ( ) )
					&& ! StringUtils.isBlank ( image.getProp ( ) )) 
				FileUploadUtil.removeFile ( image.getProp ( )  ) ; 
			if ( ! StringUtils.isEmpty ( image.getPropsm ( ) )
					&& ! StringUtils.isBlank ( image.getPropsm ( ) ) )
				FileUploadUtil.removeFile ( image.getPropsm ( ) ) ; 
			proImageDao.removeProImageById ( id ) ;
			
			Context.doCommpass4ProImage ( image.getProId ( ) , image.getProType ( ) ) ;
		}
		
		
	}	
	
	public void removeProImage( ProImage vo ) throws Exception {
		List< ProImage  > images = getProImageList ( vo )  ; 
		for ( ProImage image : images ) {
			if ( ! StringUtils.isEmpty ( image.getProp ( ) )
					&& ! StringUtils.isBlank ( image.getProp ( ) )) 
				FileUploadUtil.removeFile ( image.getProp ( )  ) ; 
			if ( ! StringUtils.isEmpty ( image.getPropsm ( ) )
					&& ! StringUtils.isBlank ( image.getPropsm ( ) ) )
				FileUploadUtil.removeFile ( image.getPropsm ( ) ) ; 			
		}
		proImageDao.removeProImage ( vo ) ; 
		
		//
		Context.doCommpass4ProImage ( vo.getProId ( ) , vo.getProType ( ) ) ;
	}
	
	public void removeProImageByColor( ProImage vo ) throws Exception {
		List< ProImage  > images = getProImageList ( vo )  ; 
		for ( ProImage image : images ) {
			if ( ! StringUtils.isEmpty ( image.getProp ( ) )
					&& ! StringUtils.isBlank ( image.getProp ( ) )) 
				FileUploadUtil.removeFile ( image.getProp ( )  ) ; 
			if ( ! StringUtils.isEmpty ( image.getPropsm ( ) )
					&& ! StringUtils.isBlank ( image.getPropsm ( ) ) )
				FileUploadUtil.removeFile ( image.getPropsm ( ) ) ; 			
		}
		proImageDao.removeProImageByColor ( vo ) ; 
		
		Context.doCommpass4ProImage ( vo.getProId ( ) , vo.getProType ( ) ) ;
	}
	
	
	
	
	public void updateProImageWorkFlg ( Long proId , Byte color , Byte proType ,
			Byte workflg ) throws Exception {
		if ( NumberUtils.isEmptyOrZero ( color )
				|| NumberUtils.isEmptyOrZero ( proId )
				|| NumberUtils.isEmptyOrZero ( proType ) 
				|| workflg == null  )
			throw new Exception( "proId is NULL OR proType is NULL OR color is NULL !" ) ; 
		
		List < ProImage > proImages = getProImageList ( new ProImage ( proId ,
				proType , color ) );
		for ( ProImage proImage : proImages ) {
			proImage.setWorkflg ( workflg );
			updateProImage ( proImage );
		}
		
		Context.doCommpass4ProImage ( proId , proType ) ;
	}
	
	
	public List< ProImage > getProImageByColor( ProImage vo  ) throws Exception {
		return proImageDao.getProImageByColor ( vo ) ; 
	}
	
	
	public List< ProImage > getProImageColorsByProIdsCacheModel( ProImage vo ) throws Exception {
		return proImageDao.getProImageColorsByProIdsCacheModel ( vo ) ;
	}
	
	public List< ProImage > getProImagesByProIdaTypeCacheModel( ProImage vo  ) throws Exception { 
		return proImageDao.getProImagesByProIdaTypeCacheModel ( vo ) ;
	}
}
